/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.common.json;

import com.entities.EventComment;
import com.entities.EventInfo;
import com.entities.EventLibrary;
import com.entities.EventRegistration;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author Tuan Long
 */
public class EventManagerJson {

    /**
     * @EventManagerJson.convertJsonStringToEventRegistrationObjectForFillRegisteredTable
     * @param json
     * @return EventRegistration
     */
    public static EventRegistration convertJsonStringToEventRegistrationObjectForFillRegisteredTable(String json) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);
            String eventname = obj.get("eventname").toString();
            String username = obj.get("username").toString();
            String registered = obj.get("registered").toString();
            EventRegistration er = new EventRegistration(eventname, username, registered);
            return er;
        } catch (ParseException ex) {
            Logger.getLogger(EventManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static EventRegistration convertJsonStringToEventRegistrationObjectForFillTotalRegistered(String json) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);            
            String username = obj.get("username").toString();
            String total = obj.get("total").toString();
            EventRegistration er = new EventRegistration(username, total);
            return er;
        } catch (ParseException ex) {
            Logger.getLogger(EventManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static EventLibrary convertJsonStringToEventRegistrationObjectForFillTotalEventTypeTable(String json) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);            
            String eventtype = obj.get("eventtype").toString();
            String total = obj.get("total").toString();
            EventLibrary el = new EventLibrary();
            el.setEventType(eventtype);
            el.setTotal(total);
            return el;
        } catch (ParseException ex) {
            Logger.getLogger(EventManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static EventComment convertJsonStringToEventCommentForRedEventComments(String json) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);            
            String eventID = obj.get("eventID").toString();
            String eventName = obj.get("eventName").toString();
            String usercomment = obj.get("usercomment").toString();
            String contentcomment = obj.get("comment").toString();
            String commented = obj.get("commented").toString();
            EventComment ec = new EventComment(eventID, eventName, usercomment, contentcomment, commented);
            return ec;
        } catch (ParseException ex) {
            Logger.getLogger(EventManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static EventLibrary convertJsonStringToEventLibraryForGetEventContentById(String json) {
        try {            
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);            
            String eventID = obj.get("eventID").toString();
            int id = Integer.parseInt(eventID);
            String eventName = obj.get("eventName").toString();
            String eventType = obj.get("eventType").toString();
            String description = obj.get("description").toString();
            String start = obj.get("start").toString();
            String end = obj.get("end").toString();
            String totalcomments = obj.get("comments").toString();
            EventLibrary el = new EventLibrary();
            el.setEventID(id);
            el.setEventName(eventName);
            el.setEventType(eventType);
            el.setDescription(description);
            el.setTimeStart(start);
            el.setTimeEnd(end);
            el.setTotalComments(totalcomments);
            return el;
        } catch (ParseException ex) {
            Logger.getLogger(EventManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public static EventInfo convertJsonStringToEventInfoForGetEventContentById2(String json) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);            
            String avatar = obj.get("avatar").toString();
            String shortdes = obj.get("shortdes").toString();
            String created = obj.get("created").toString();
            String address = "Ha Noi, Viet Nam";
            if(obj.get("address") != null){
                address = obj.get("address").toString();
            }
            EventInfo ei = new EventInfo();
            ei.setAvatar(avatar);
            ei.setShortdes(shortdes);
            ei.setCreated(created);
            ei.setAddress(address);            
            return ei;
        } catch (ParseException ex) {
            Logger.getLogger(EventManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static EventLibrary convertJsonStringToEventLibraryForGetTop10EventIdAndNameByEventTag(String json) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);            
            String eventId = obj.get("eventID").toString();
            String eventName = obj.get("eventName").toString();
            int id = Integer.parseInt(eventId);
            EventLibrary el = new EventLibrary();
            el.setEventID(id);
            el.setEventName(eventName);
            return el;
        } catch (ParseException ex) {
            Logger.getLogger(EventManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static EventLibrary convertJsonEventCommingDataToEventLibrary(String json) {
        System.out.println("JSON: "+json);
        try {
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);            
            String eventID = obj.get("eventID").toString();
            int id = Integer.parseInt(eventID);
            String eventName = obj.get("eventName").toString();
            String eventType = obj.get("eventType").toString();
            String description = obj.get("description").toString();
            String start = obj.get("start").toString();
            String end = obj.get("end").toString();
            String avatar = obj.get("avatar").toString();
            String shortdes = obj.get("shortDes").toString();
            String created = obj.get("created").toString();
            String address = "Ha Noi, Viet Nam";
            if(obj.get("address") != null){
                address = obj.get("address").toString();
            } 
            String total = "0";
            if(obj.get("total") != null){
                total = obj.get("total").toString();
            } 
            String map = "https://www.google.com/maps/embed/v1/search?q=+" + address + "&key=AIzaSyAxLVqBfAWr9zYdN3cfdZYCMov7l-o8HrI";
            EventLibrary el = new EventLibrary();
            el.setEventID(id);
            el.setEventName(eventName);
            el.setEventType(eventType);
            el.setDescription(description);
            el.setTimeStart(start);
            el.setTimeEnd(end);
            el.setAvatar(avatar);
            el.setShortdes(shortdes);
            el.setCreated(created);
            el.setAddress(address);
            el.setTotal(total);
            el.setMap(map);
            return el;
        } catch (ParseException ex) {
            Logger.getLogger(EventManagerJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    

}
